﻿using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using SweetSurvey.Models;
using SweetSurvey.Models.Repositories;

namespace SweetSurvey.Controllers
{
    [Authorize(Roles = AccountMembershipService.AdministratorRoleName)]
    public class AdministratorController : ControllerBase
    {
        #region Fields

        private ISurveyRepository surveyRepository;
        private IParticipatorRepository participatorRepository;

        #endregion

        #region Constructors

        public AdministratorController(
            ISurveyRepository surveyRepository,
            IParticipatorRepository participatorRepository)
        {
            this.surveyRepository = surveyRepository;
            this.participatorRepository = participatorRepository;
        }

        #endregion

        //
        // GET: /Administrator/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Surveys()
        {
            return View(this.surveyRepository.GetAllSurveys());
        }

        [HttpPost]
        public ActionResult DeleteSurveys(string surveyIds)
        {
            var list = (new JavaScriptSerializer()).Deserialize<List<string>>(surveyIds);
            foreach (var id in list)
            {
                this.surveyRepository.Delete(id);
            }
            return Content("success");
        }

        public ActionResult Participators()
        {
            return View(this.participatorRepository.GetAllParticipators());
        }

        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);

            this.Logger.Info("Executing administrative action: {0}", filterContext.ActionDescriptor.ActionName);
        }
    }
}
